import Axios from 'axios'
import { showFailToast } from 'vant';

const service = Axios.create({
  baseURL: import.meta.env.VITE_BASIC_URL,
  timeout: 5000 // request timeout
});

// 实例添加请求拦截器
service.interceptors.request.use(
  config=> {
  
    /*if (store.getters.token) {
      config.headers['Authorization'] = 'Bearer ' + getToken()
    }*/
    config.headers['Authorization'] = 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJzYWZwIiwidXNlclBlcnNvbk5hbWUiOiJzYWZwIiwidXNlck9yZyI6Im9yZzAwMDAwMDAzIiwidG9rZW5TeXN0ZW1Vc2VySWQiOiIxN2NmYmpBREc4SWdOQUZQV1AiLCJ1c2VyVmVyc2lvbiI6MywiZXhwIjoxNjc5NjQzODIyLCJ1c2VyTmFtZSI6InNhZnAiLCJ1c2VySWQiOiIxN2NmYmpnNTZIZUJFUnVGaTQiLCJqdGkiOiJVcW5DZWh2RGRqNyJ9.OisL3wQ9sLip6ejtG6FwImZe_XNDKJRLol5t_VXO3xQ';
    
    // if (config.method === 'post') {}
    
    // return Promise.resolve(config);
    return config
  },
  
  error=> {
    // 对请求错误做处理...
    return Promise.reject(error);
  }
);


// 实例添加响应拦截器
service.interceptors.response.use(
  response => {
    return response.data
    // return response;
  },
  error => {
    let message = '网络异常。';
    if (error.response) {
      if (error.response.data && error.response.data.message) {
        message = error.response.data.message;
      } else {
        switch (error.response.status) {
          case 401:
            message = '没有操作权限。';
            break;
          case 500:
            message = '服务器异常。';
            break;
          default:
            break;
        }
      }
    }
    showFailToast(message);
    return Promise.reject(error.response);
  }
);


export default service;
